#include "log_in.h"
#include "ui_log_in.h"
#include <QApplication>
#include <QMessageBox>
#include <QLineEdit>
log_in::log_in(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::log_in)
{
    ui->setupUi(this);
    ui->password->setEchoMode(QLineEdit::Password);
    setWindowFlags(Qt::Dialog);
    setFixedSize(400, 300);

}

log_in::~log_in()
{
    delete ui;
}

void log_in::on_pushButton_clicked()  //student登录按钮点击触发
{
    if(ui->number->toPlainText()==NULL || ui->password->text()==NULL)  //判断账号密码是否某一为空
    {
        QMessageBox::about(NULL,"tip","number and password can not be null!"); //提示密码或账号不能为空
    }
    else
    {
        QSqlQuery query;
        QString sql= "select password,sname from student where sno='"+ui->number->toPlainText()+"'";
        QString s_name;
        query.exec(sql);
        query.next();
        if(query.value(0).toString()==ui->password->text())  //判断账号密码是否匹配
        {
            this->hide();
            s_name=query.value(1).toString();
            s.setWindowTitle("Welcome! "+s_name);  //更改窗口名称
            s.set_sno(ui->number->toPlainText());  //将学号存储在sno中
            s.initialise();  //显示学生选课信息
            QDesktopWidget *deskdop = QApplication::desktop();
            s.move((deskdop->width() - this->width())/2, (deskdop->height() - this->height())/2);  //使窗口居中
            s.show();
        }
        else
        {
            QMessageBox::about(NULL,"error","please input correct number and password!");    //提示账号密码不匹配
        }

    }



}

void log_in::on_pushButton_3_clicked()   //teacher登录按钮点击触发
{
    if(ui->number->toPlainText()==NULL || ui->password->text()==NULL)
    {
        QMessageBox::about(NULL,"tip","number and password can not be null!");
    }
    else
    {
        QSqlQuery query;
        QString sql= "select password,tname from teacher where tno='"+ui->number->toPlainText()+"'";
        QString t_name;
        query.exec(sql);
        query.next();
        if(query.value(0).toString()==ui->password->text())
        {
            this->hide();
            t_name=query.value(1).toString();
            t.setWindowTitle("Welcome! "+t_name);
            t.set_tno(ui->number->toPlainText());
            QDesktopWidget *deskdop = QApplication::desktop();
            t.move((deskdop->width() - this->width())/2, (deskdop->height() - this->height())/2);
            t.show();
        }
        else
        {
            QMessageBox::about(NULL,"error","please input correct number and password!");
        }
    }

}

void log_in::on_pushButton_2_clicked()  //sys登录按钮触发
{
    if(ui->number->toPlainText()==NULL || ui->password->text()==NULL)
    {
        QMessageBox::about(NULL,"tip","number and password can not be null!");
    }
    else
    {
        QSqlQuery query;
        QString sql= "select password,tname from teacher where tno='"+ui->number->toPlainText()+"'";
        QString t_name;
        query.exec(sql);
        query.next();
        if(query.value(0).toString()==ui->password->text())
        {
            this->hide();
            t_name=query.value(1).toString();
            s_.setWindowTitle("Welcome! "+t_name);
            QDesktopWidget *deskdop = QApplication::desktop();
            s_.move((deskdop->width() - this->width())/2, (deskdop->height() - this->height())/2);
            s_.show();
        }
        else
        {
            QMessageBox::about(NULL,"error","please input correct number and password!");
        }
    }

}
